//var fpdr = {
//    setBtnStatus:function () {
//        var row = $('#grid_fpdr').datagrid('getSelected');
//        if (row && row.id) {
//            $('#btn_fpdr_lead').show();
//        } else {
//            $('#btn_fpdr_lead').hide();
//        }
//    }
//}

var fpdr = {
    setBtnStatus: function () {
        var row = $('#grid_fpdr').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                $("#btn_fpdr_insert").show();//导入
                $("#btn_fpdr_combine").show();//合并
                $("#btn_fpdr_cx").hide();//撤销
                $("#btn_fpdr_cz").hide();//重置
            }
            if (row.czzt == 'drcg') {
                $("#btn_fpdr_insert").hide();//导入
                $("#btn_fpdr_combine").hide();//合并
                $("#btn_fpdr_cx").show();//撤销
                $("#btn_fpdr_cz").hide();//重置
            }
            if (row.czzt == 'drsb') {
                $("#btn_fpdr_insert").hide();//导入
                $("#btn_fpdr_combine").hide();//合并
                $("#btn_fpdr_cx").hide();//撤销
                $("#btn_fpdr_cz").show();//重置
            }
            if (row.czzt == 'sccg') {
                $("#btn_fpdr_insert").show();//导入
                $("#btn_fpdr_combine").show();//合并
                $("#btn_fpdr_cx").hide();//撤销
                $("#btn_fpdr_cz").hide();//重置
            }
            if (row.czzt == 'scsb') {
                $("#btn_fpdr_insert").hide();//导入
                $("#btn_fpdr_combine").hide();//合并
                $("#btn_fpdr_cx").hide();//撤销
                $("#btn_fpdr_cz").show();//重置
            }
        } else {
            $("#btn_fpdr_insert").hide();//导入
            $("#btn_fpdr_combine").hide();//合并
            $("#btn_fpdr_cx").hide();//撤销
            $("#btn_fpdr_cz").hide();//重置
        }
    }
}
$(function () {
    $("#fpdr_glqy").qyjgzcombobox();
    //表格初始加载
    $('#grid_fpdr').datagrid({
        url:$.webPath() + "/cwgl/fpdr/fpdrList",
        toolbar:"#toolbar_fpdr",
        fitColumns:true,
        singleSelect:true,
        selectOnCheck:false,
        checkOnSelect:false,
        pageNumber: 1,
        pagination:true,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {checkbox: true},
            {field:'id', title:'ID', hidden:true},
            {field:'jsdh', title:'结算单号', width:40,align: 'center'},
            {field:'gs_jc', title:'申报公司', width:30,align: 'center'},
            {field:'cggs_jc', title:'业务公司', width:30,align: 'center'},
            {field:'type', title:'类型', width:15,align: 'center',
                formatter:function (value) {
                    if (value == '0')
                        return '进项';
                    if (value == '1')
                        return '销售';
                    if (value == '2')
                        return '物流';
                }
            },
            {field:'glqy_qc', title:'客户名称', width:30,align: 'center'},
            {field:'fpze', title:'发票总额(元)', width:30,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }
            },
            {field:'ddfy', title:'代垫运费(元)', width:30,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }
            },
            {field:'fpzl', title:'发票种类', width:18,align: 'center',
                formatter:function (value) {
                    if (value == '0')
                        return '增票';
                    if (value == '1')
                        return '普票';
                    if (value == '2')
                        return '其他';
                    if (value == '3')
                        return '另开票';
                }
            },
            {field:'hm', title:'发票号码', width:35,align: 'center'},
            {field:'sqr_xm', title:'申请人', width:15,align: 'center'},
            {field:'kprq', title:'开票日期', width:30,align: 'center'},
            {field:'kpr_xm', title:'开票人', width:15,align: 'center'},
            {field:'sqrq', title:'申请日期', width:30,align: 'center'},
            {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
            {field: 'czzt', title: '状态' ,align: 'center',width: 10, formatter: function (value, row, index) {
                if (value == 'wdr')
                    return "未导入";
                if (value == 'drcg')
                    return "导入成功";
                if (value == 'drsb')
                    return "导入失败";
                if (value == 'sccg')
                    return "删除成功";
                if (value == 'scsb')
                    return "删除失败";
                return "";
            }}
        ]],
        onLoadSuccess:function(){
            fpdr.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
            fpdr.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
   //导入按钮
    $("#btn_fpdr_insert").click(function () {
        fpdr_insert();
    });

    //重置
    $("#btn_fpdr_cz").click(function () {
        fpdr_cz();
    });

    //查询按钮
    $("#btn_fpdr_search").click(function () {
        fpdr_search();
    });
    //发票合并
    $("#btn_fpdr_combine").click(function () {
        fpdr_combine();
    });

    //撤销
    $("#btn_fpdr_cx").click(function () {
        fpdr_cx();
    });


    $("#fpdr_gs").gsjgtree(1);
});
function fpdr_combine(){
    var datas = $('#grid_fpdr').datagrid('getChecked');
        if (datas && datas.length > 1) {
        var glqyid = datas[0]["glqy_id"];
        var paramString = "";
        for (var i = 0; i < datas.length; i++) {
            if(datas[i]["type"] !=2 && glqyid != datas[i]["glqy_id"]){
                $.messager.alert("提示","结算单位必须是同一个企业！","warning");
                return;
            }
            paramString += datas[i].id+",";
        }
        paramString = paramString.substring(0,paramString.length-1);
        $.messager.confirm('提示', '是否进行结算单合并', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/cwgl/fpdr/fpdr_combine",
                    dataType: 'json',
                    type: 'POST',
                    data: {jsdIdList:paramString},
                    beforeSend: function () {
                        $("#layout_fpdr").mask();
                    },
                    success:function(data){
                        $("#layout_fpdr").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_fpdr').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择两个以上的结算单！", "warning");
    }
}

//弹票据贴现窗口
function fpdr_insert() {
    var row = $('#grid_fpdr').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"发票导入",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/fpdr/fpdr_insert_content?jsdId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/fpdr/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"fpdrSystemDate":$("#fpdr_systemDate").datebox("getValue"),
                                "attachment":$("#fpdr_attachment").val(),
                                "bz":$("#fpdr_bz").val(),
                                "jsdId":$("#jsd_id").val()},
                            beforeSend: function () {
                                if($("#fpdr_attachment").val()=="" || $("#fpdr_attachment").val()==null){
                                    $.messager.alert("提示","请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                                if($("#fpdr_systemDate").datebox("getValue")=="" || $("#fpdr_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $("#layout_fpdr").mask("hide");
                                $(win).dialog('close');
                                if (msg.responseflag == 0){
                                    $('#grid_fpdr').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#grid_fpdr').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}
//贴现承兑重置
function fpdr_cz(){
    var row = $('#grid_fpdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该发票？', function (r) {
            if (r) {
                var index = $('#grid_fpdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/fpdr/rest_voucher?jsdId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_fpdr").mask();
                    },
                    success:function(msg){
                        $("#layout_fpdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_fpdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_fpdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}

//贴现承兑撤销
function fpdr_cx(){
    var row = $('#grid_fpdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该发票？', function (r) {
            if (r) {
                var index = $('#grid_fpdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/fpdr/revoke_voucher?jsdId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_fpdr").mask();
                    },
                    success:function(msg){
                        $("#layout_fpdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_fpdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_fpdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的发票！", "warning");
    }
}



function fpdr_search() {
//    var glqyId = $('#fpdr_glqy').val() == null ? null : $('#fpdr_glqy').val();
//    var glqyQc = $('#fpdr_glqy').attr("_text") == null ? null : $('#fpdr_glqy').attr("_text");
//    if(glqyQc==""){
//        glqyId = null;
//    }
//    var gsId = $('#fpdr_gs').combotree('getValue') == null ? null : $('#fpdr_gs').combotree('getValue');
//    var gsJc = $('#fpdr_gs').combotree('getText') == null ? null : $('#fpdr_gs').combotree('getText');
//    var fpdrzlId = $('#fpdr_fpdrzl').combobox('getValue') == null ? null : $('#fpdr_fpdrzl').combobox('getValue');
//    var sqrqStart = $('#fpdr_sqrq_start').datebox('getValue');
//    var sqrqEnd = $('#fpdr_sqrq_end').datebox('getValue');
//    var fpdrZt = $('#fpdr_zt').combobox('getValue') == null ? null : $('#fpdr_zt').combobox('getValue');
//    var type = $('#fpdr_type').combobox('getValue') == null ? null : $('#fpdr_type').combobox('getValue');
   $('#grid_fpdr').datagrid('load',
        {
            jsdh:$("#toolbar_fpdr input[name='jsdh']").val()||'',
            glqyId:$("#toolbar_fpdr input[name='glqy']").val()||'',
            glqyQc:$("#fpdr_glqy").attr("_text")||'',
            gsId:$("#toolbar_fpdr input[name='gs']").val()||'',
            gsJc:$("#fpdr_gs").combotree("getText")||'',
            fpzlId:$("#toolbar_fpdr input[name='fpdrzl']").val()||'',
            sqrqStart:$("#toolbar_fpdr input[name='fpdr_sqrq_start']").val()||'',
            sqrqEnd:$("#toolbar_fpdr input[name='fpdr_sqrq_end']").val()||'',
            fpdrZt:$("#toolbar_fpdr input[name='shzt']").val()||'',
            type:$("#toolbar_fpdr input[name='type']").val()||''
        }
    );
}
